//set main namespace
goog.provide('GameScene');


//get requirements
goog.require('lime.Director');
goog.require('lime.Scene');
goog.require('lime.Layer');
goog.require('lime.Circle');
goog.require('lime.Label');
goog.require('lime.animation.Spawn');
goog.require('lime.animation.FadeTo');
goog.require('lime.animation.ScaleTo');
goog.require('lime.animation.MoveTo');

var layers = new Array();// MapLayer,UILayer,MoveLayer,EffectLayer;
var UIs = new Array;// map,hammer,wall;
var enemyCount = 3;
var TimerID;
var hammerOn = false;
var wallOn = false;
var charas = new Array();// enemy:0~enemyCount people: people
var lbl;
// map array
var mapArr = 
[
	[18,18,18,18,18,18,18,18,18,18,18,18,55,55,18],
	[18,18,18,17,17,17,17,17,17,17,17,17,55,55,18],
	[18,18,17,17,17,17,18,18,17,17,17,17,55,55,18],
	[18,17,17,17,18,18,18,18,18,17,17,55,55,17,18],
	[18,17,17,18,22,23,23,23,24,18,17,55,55,17,18],
	[18,17,17,18,25,28,26,79,27,18,55,55,17,17,18],
	[18,17,17,17,17,10,11,12,18,18,55,55,17,17,18],
	[18,18,17,17,10,16,16,16,11,55,55,17,17,17,18],
	[18,18,17,17,10,16,16,16,16,15,15,17,17,17,18],
	[18,18,18,18,18,18,18,18,18,55,55,18,18,18,18]
];

// character array 0:nothing 1:enemy 2:people
var charaArr = 
[
	[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
	[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
	[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
	[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
	[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
	[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
	[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
	[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
	[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
	[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
];

function position() {
	this.x = 0;
	this.y = 0;
}

function walkAble(x, y) {
	if(mapArr[y][x] >= 18)
		return false;
	else
		return true;
}

// map position to real position
function mapPositonToRealPosition(p) {
	var width = UIs['map'].getSize().width;
	var height = UIs['map'].getSize().height;
	p.x = Math.floor(width/15)*(p.x+0.5)+UIs['map'].getPosition().x-width/2;
	p.y = Math.floor(height/10)*(p.y+0.5)+UIs['map'].getPosition().y-height/2;
}

// real pasition to map position
function realPositonToMapPosition(p) {
	var width = UIs['map'].getSize().width;
	var height = UIs['map'].getSize().height;
	p.x = Math.floor((p.x-(UIs['map'].getPosition().x-width/2))/(width/15));
	p.y = Math.floor((p.y-(UIs['map'].getPosition().y-height/2))/(height/10));
}

// character struction
function Chara(){
	this.charaSprite = new lime.Sprite();
	// 0=people 1=enemy0 2=enemy1
	this.type = 0;
	this.x = 0;
	this.y = 0;
	this.destinationX = -1;
	this.destinationY = -1;
	this.speed = 0;
	this.isDead = false;
	this.charaSprite.setHidden(true);
}

function initLayers(scene){
	layers['MapLayer'] = new lime.Layer().setPosition(0,0);
	layers['UILayer'] = new lime.Layer().setPosition(0,0);
	layers['MoveLayer'] = new lime.Layer().setPosition(0,0);
	layers['EffectLayer'] = new lime.Layer().setPosition(0,0);

	scene.appendChild(layers['MapLayer']);
	scene.appendChild(layers['UILayer']);
	scene.appendChild(layers['MoveLayer']);
	scene.appendChild(layers['EffectLayer']);
	
	lbl = new lime.Label().setSize(160,0).setFontSize(30).setPosition(100, 100);
	scene.appendChild(lbl);
}

function initMap(){
	UIs['map'] = new lime.Sprite().setSize(480,320).setFill('img/map.png').setPosition(512,384);
	layers['MapLayer'].appendChild(UIs['map']);
}

function initChara(){
	for (var k = 0; k < enemyCount; ++k)
		while(addEnemy(k)!=true);

	while(addPeople()!=true);
	charas['hammer'] = new Chara();
	charas['hammer'].charaSprite.setSize(20,10).setFill('img/hammer.png')
	charas['hammer'].speed = 1;
	charas['wall'] = new Chara();
}

// add the people
function addPeople()
{
	var x = Math.floor(Math.random()*15);
	var y = Math.floor(Math.random()*10);
	var width = UIs['map'].getSize().width;
	var height = UIs['map'].getSize().height;

	if(!walkAble(x, y))
		return false;
	var p = new position();
	p.x = x, p.y = y;
	mapPositonToRealPosition(p);
	
	charas['people'] = new Chara();
	charas['people'].charaSprite.setSize(width/15,height/10).setFill('img/people.png').setPosition(p.x,p.y);
	charas['people'].x = x;
	charas['people'].y = y;
	charas['people'].speed = 1;
	charas['people'].charaSprite.setHidden(false);
	
	charaArr[y][x] = 2;
	layers['MoveLayer'].appendChild(charas['people'].charaSprite);
	
	return true;
}

// add an enemy
function addEnemy(k){
	var x = Math.floor(Math.random()*15);
	var y = Math.floor(Math.random()*10);
	var width = UIs['map'].getSize().width;
	var height = UIs['map'].getSize().height;

	if(!walkAble(x, y))
		return false;

	var p = new position();
	p.x = x, p.y = y;
	mapPositonToRealPosition(p);
	var r = Math.floor(Math.random()*2);
	
	charas[k] = new Chara();
	charas[k].x = x;
	charas[k].y = y;
	if(r == 0) {
		charas[k].charaSprite.setSize(width/15,height/10).setFill('img/enemy0.png').setPosition(p.x,p.y);
		charas[k].speed = 1;
	} else {
		charas[k].charaSprite.setSize(width/15,height/10).setFill('img/enemy1.png').setPosition(p.x,p.y);
		charas[k].speed = 2;
	}
	charaArr[y][x] = 1;
	layers['MoveLayer'].appendChild(charas[k].charaSprite);
	charas[k].charaSprite.setHidden(false);
	
	return true;
}

// init the UI
function initUI(){
	UIs['hammer'] = new lime.Sprite().setSize(80,40).setFill('img/hammerUI.png').setPosition(472,584);
	UIs['wall'] = new lime.Sprite().setSize(80,40).setFill('img/wallUI.png').setPosition(552,584);
	layers['UILayer'].appendChild(UIs['hammer']);
	layers['UILayer'].appendChild(UIs['wall']);
}

// init the controllers
function initContrl(){
	goog.events.listen(UIs['map'],['mousedown','touchstart'],function(e){

		if(hammerOn == true) {
			charas['hammer'].x = charas['people'].x;
			charas['hammer'].y = charas['people'].y;
			
			var p = new position();
			p.x = e.position.x+UIs['map'].getPosition().x, p.y = e.position.y+UIs['map'].getPosition().y;
			realPositonToMapPosition(p);

			var ata=Math.atan((charas['hammer'].y - p.y)/(p.x - charas['hammer'].x))/3.14*180;
			if(charas['hammer'].y < p.y){
				if(Math.abs(ata)>45) {
					lbl.setText('down');
					charas['hammer'].destinationX = charas['hammer'].x;
					charas['hammer'].destinationY = 10;
				} else if (charas['hammer'].x < p.x) {
					charas['hammer'].destinationX = 15;
					charas['hammer'].destinationY = charas['hammer'].y;
					lbl.setText('right');
				} else {
					charas['hammer'].destinationX = 0;
					charas['hammer'].destinationY = charas['hammer'].y;
					lbl.setText('left');
				}
			} else {
				if(Math.abs(ata)>45) {
					lbl.setText('up');
					charas['hammer'].destinationX = charas['hammer'].x;
					charas['hammer'].destinationY = 0;
				} else if (charas['hammer'].x < p.x) {
					charas['hammer'].destinationX = 15;
					charas['hammer'].destinationY = charas['hammer'].y;
					lbl.setText('right');
				} else {
					charas['hammer'].destinationX = 0;
					charas['hammer'].destinationY = charas['hammer'].y;
					lbl.setText('left');
				}		
			}
			hammerOn = false;
			charas['hammer'].charaSprite.setHidden(false);
			return;
		}
		
		if(wallOn == true) {
			wallOn = false;
			return;		
		}

		var p = new position();
		p.x = e.position.x+UIs['map'].getPosition().x, p.y = e.position.y+UIs['map'].getPosition().y;
		realPositonToMapPosition(p);
		charas['people'].destinationX = p.x;
		charas['people'].destinationY = p.y;
    });
	goog.events.listen(UIs['hammer'],['mousedown','touchstart'],function(e){
		hammerOn = true;
    });
	goog.events.listen(UIs['wall'],['mousedown','touchstart'],function(e){
		wallOn = true;
    });
}

function getNextLocation(c)
{
	var width = UIs['map'].getSize().width;
	var height = UIs['map'].getSize().height;
	
	if(c.destinationX==-1 || c.destinationY==-1)
		return;
	
	if(c.x!=c.destinationX) {
		if(c.destinationX>c.x&&
		walkAble(c.x+1,c.y))
		{
			c.x++;
		}
		else if(c.destinationX<c.x&&
		walkAble(c.x-1,c.y))
		{
			c.x--;
		}
	}
	
	if(c.y!=c.destinationY) {
		if(c.destinationY>c.y&&
		walkAble(c.x,c.y+1))
		{
			c.y++;
		}			
		else if(c.destinationY<c.y&&
		walkAble(c.x,c.y-1))
		{
			c.y--;
		}
	}
}

function moveChara(c)
{
	var p = new position();

	p.x = c.x;
	p.y = c.y;
	mapPositonToRealPosition(p);

	var move = new lime.animation.MoveTo(p.x, p.y).setDuration(1);
	c.charaSprite.runAction(move);
}

function moveAll()
{
	// get people next Location
	getNextLocation(charas['people']);
	
	// get all enemy next Location
	for (var i = 0; i < enemyCount; ++i) 
	{
		charas[i].destinationX = charas['people'].x;
		charas[i].destinationY = charas['people'].y;
		
		for (var j = 0; j < charas[i].speed; ++j){
			getNextLocation(charas[i]);
		}
	}
	// get all weapons next Location
	getNextLocation(charas['hammer']);
	
	// move all character to next Location
	moveChara(charas['people']);
	for (var i = 0; i < enemyCount; ++i) 
	{
		moveChara(charas[i]);		
	}
	moveChara(charas['hammer']);
}

// init function
GameScene.init = function(scene){
	initLayers(scene);
	initMap();
	initChara();
	initUI();
	initContrl();
}

function TimerStart(func,delay)
{
	TimerID = window.setInterval(
		function()
		{
			if(!func())
			{
				window.clearInterval(TimerID);
			};
		},
		delay
	);
}

function rePaint()
{
	moveAll();
	// todo ifOver()?;
	// todo moveUI
	
	return true;
}

// start game function
GameScene.start = function(){
	TimerStart(rePaint, 500);
}

// start game function
GameScene.stop = function(){
	TimerStart(null, 1);
}